#
# dump-feature-coordinates-to-csv.py
#
# Dump the X/Y coordinates of features (assuming point data)
# from an input shapefile into a CSV table that can later be imported
# into Excel / OpenOffice or dumped as input into a Garmin GPS unit.
#

import sys, os
import random

# global options

USING_RANDOMIZATION = 0
numRndPts = 0

# local imports of external functions

from get_random_elements import *

print "\n dump-feature-coordinates-to-csv"
print " ----------------------------------------------------------------------"
  
if (len(sys.argv) < 2):
  print " Dump the X/Y coordinates of point features in an input shapefile"
  print " into a CSV table that can be imported into Excel / OpenOffice "
  print " or dumped as waypoint input into a Garmin GPS unit."
  print "  "
  print " Usage: ./dump-feature-coordinates <input shapefile> [OPTIONS]\n"
  print " OPTIONS: --rndPoints <number of random points to retrieve>\n"
  print " -----------------------------------------------------------------------\n"
  sys.exit()
  
else:
  try:
    print " ... loading ArcPy module for ArcGIS 10"
    import arcpy
  
  except ImportError:
    print " ... import error.\n"
    sys.exit()
    
  for n in range(len(sys.argv)):
    if str(sys.argv[n]) == "--rndPoints":
		USING_RANDOMIZATION = 1
		print " ... will randomize", sys.argv[n+1], "points from input file." 
		numRndPts = int(sys.argv[n+1])
          


# Find our CWD and create our CSV file
shapefile_directory = sys.argv[1][:sys.argv[1].rfind('\\')]

csvFile = open(shapefile_directory  + "\\garminImport.csv", "w")

#
# Build a search cursor to manipulate input data
#

try:
  # build a geometry description of our input shapefile
  desc = arcpy.Describe(sys.argv[1])
  shapefieldname = desc.ShapeFieldName
  # build a search cursor object to iterate through
  rows = arcpy.SearchCursor(sys.argv[1])
  
except IOError:
  print "\n ... failed to load shapefile : " + str(sys.argv[1]) 
  print " ... quit.\n"
  sys.exit()

# 
# Create a feature list to hold X/Y values retrieved from points in our input shapefile.
#

featureList = [[0 for x in xrange(4)] for x in xrange(int(arcpy.GetCount_management(sys.argv[1]).getOutput(0)))] 
lineCnt = 0 

for row in rows:
	
    feat = row.getValue(shapefieldname)
    pnt = feat.getPart()


    featureList[lineCnt][0] = pnt.X
    featureList[lineCnt][1] = pnt.Y
    featureList[lineCnt][2] = row.getValue("Ear_Tag")
    featureList[lineCnt][3] = row.getValue("Research_Y")

    lineCnt+=1

print " ... retrieved", lineCnt, "features."

#
# Write lines describing X/Y coordinates, Year, and Ear_Tag to a CSV file.  Randomize, if asked.
#

if USING_RANDOMIZATION == 1:
  valuesToWrite = getRandomElements(numRndPts, featureList)
else:
  valuesToWrite = featureList
  
for element in valuesToWrite:
	csvFile.write(str(element[0]) + "," + str(element[1]) + "," + '"' + "Ear Tag: " + str(element[2]) + '"' + "," + '"' + "Year: " + str(element[3]) + '"' + "\n")

print " ... exported to garmin CSV file : " + shapefile_directory  + "\\garminImport.csv"
